package com.trkj.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IYzrStorehouseDAO;
import com.trkj.dao.jpa.LmbConsumableInventDAO;
import com.trkj.dao.jpa.LmbConsumableInventDetailDAO;
import com.trkj.dao.jpa.LmbConsumptiveStockDAO;
import com.trkj.dao.jpa.LmbTbstaffDAO;
import com.trkj.pojos.LmbConsumableInvent;
import com.trkj.pojos.LmbConsumableInventDetail;
import com.trkj.pojos.LmbConsumptiveStock;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrStorehouse;
import com.trkj.vo.LmbCkPdVo;
import com.trkj.vo.LmbCkVo;
import com.trkj.vo.LmbHcpdVo;



@Service
@Transactional
public class LmbCkpdService {
	@Autowired
	private LmbConsumptiveStockDAO consumptiveStockDAO;
	@Autowired
	private IYzrStorehouseDAO storehouseDAO;
	@Autowired
	private LmbConsumableInventDAO  conInventDAO;
	@Autowired
	private LmbConsumableInventDetailDAO conInventDetDAO;
	@Autowired
	private LmbOddNumbersService oddNumbersService;
	@Autowired
	private LmbTbstaffDAO tbstaffDAO;
	
	public Map<String,Object> findHcByCkId(int page,int rows,String ckId){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<LmbConsumptiveStock> list = consumptiveStockDAO.findHcByWarehouseId(pr, ckId);
		//获取总的记录条数
		long total = consumptiveStockDAO.countHcByWarehouseId(ckId);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", conStockPoToVoListToVoList(list));
		return map;
	}
	
	public LmbCkVo conStockPoToVo(LmbConsumptiveStock conStock){
		LmbCkVo ckVo = new LmbCkVo();
		ckVo.setCstockId(conStock.getCstockId());
		ckVo.setCstocCid(conStock.getCstocCid());
		ckVo.setCstocCname(conStock.getCstocCname());
		ckVo.setCstocBatchNum(conStock.getCstocBatchNum());
		ckVo.setCstocNorms(conStock.getCstocNorms());
		ckVo.setCstockProductDate(conStock.getCstockProductDate());
		ckVo.setCstockValidTill(conStock.getCstockValidTill());
		ckVo.setCstockSupplier(conStock.getCstockSupplier());
		ckVo.setCstocNum(conStock.getCstocNum());
		ckVo.setCstocWarehouseId(conStock.getCstocWarehouseId());
		YzrStorehouse storehouse = storehouseDAO.findOne(conStock.getCstocWarehouseId());
		ckVo.setCkName(storehouse.getStoreName());
		return ckVo;	
	}
	
	public List<LmbCkVo> conStockPoToVoListToVoList(List<LmbConsumptiveStock> list){
		if(list==null) return null;
		List<LmbCkVo> lmbCkVo = new ArrayList<>();
		for(LmbConsumptiveStock consStock:list){
			LmbCkVo ckVo = conStockPoToVo(consStock);
			lmbCkVo.add(ckVo);
		}
		return lmbCkVo;		
	}
	
	public List<LmbCkVo> findAllHcByCkId(String ckId){
		List<LmbConsumptiveStock> list =consumptiveStockDAO.findAllByWarehouseId(ckId);
		return conStockPoToVoListToVoList(list);
	}
	
	public Map<String,Object> findAllBycname(int page,int rows,String cName){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<LmbConsumptiveStock> list = consumptiveStockDAO.findAllByCname(pr, cName);
		//获取总的记录条数
		long total = consumptiveStockDAO.countfindAllByCname(cName);
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", conStockPoToVoListToVoList(list));
		return map;
	}
	
	
	public void pdHcKc(LmbCkPdVo ckPdVo){
		String date = oddNumbersService.rq();
		//拼接
		String name = "HP-" + date;					
		String n = conInventDAO.findMaxID(name);
		System.out.println("从数据库中读取的最大采购计划编号："+n);
		String sid =oddNumbersService.getId(name, n);
		LmbConsumableInvent conInvent = ckPdVo.getInvent();
		conInvent.setCiId(sid);
		conInvent.setCiDate(new Date());
		conInventDAO.save(conInvent);
		List<LmbConsumableInventDetail> list = ckPdVo.getInvenDet();
		if(list !=null){
			for(LmbConsumableInventDetail conInventDet:list){
				String name1 = "HPX-" + date;	
				String n1 = conInventDetDAO.findMaxID(name1);
				String sid1 = oddNumbersService.getId(name1, n1);
				conInventDet.setCidId(sid1);
				conInventDet.setCidCiId(sid);
				conInventDetDAO.save(conInventDet);
			}
		}
	}
	
	public  LmbHcpdVo pdPo2Vo(LmbConsumableInvent invent){
		LmbHcpdVo hcpdVo = new LmbHcpdVo();
		hcpdVo.setCiId(invent.getCiId());
		hcpdVo.setCiDate(invent.getCiDate());
		hcpdVo.setCiPeople(invent.getCiPeople());
		Tbstaff staff = tbstaffDAO.findOne(invent.getCiPeople().longValue());
		hcpdVo.setPdyName(staff.getStaffName());
		hcpdVo.setCiStorehouse(invent.getCiStorehouse());
		YzrStorehouse storehouse = storehouseDAO.findOne(invent.getCiStorehouse());
		hcpdVo.setPdckName(storehouse.getStoreName());
		return hcpdVo;		
	}
	
	public List<LmbHcpdVo> pdListPo2Vo(List<LmbConsumableInvent> list){
		if(list==null) return null;
		List<LmbHcpdVo> invent = new ArrayList<>();
		for(LmbConsumableInvent consumableInvent:list){
			LmbHcpdVo hcpdVo = pdPo2Vo(consumableInvent);
			invent.add(hcpdVo);
		}
		return invent;
	}
	
	public Map<String,Object> findinventByPage(int page,int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<LmbConsumableInvent> list = conInventDAO.findinventByPage(pr);
		long total = conInventDAO.countfindinventByPage();
		Map<String,Object>  map = new HashMap<>();
		map.put("total", total);
		map.put("rows", pdListPo2Vo(list));
		return map;
	}
	
	public LmbConsumableInvent findOneInvent(String id){
		LmbConsumableInvent invent = conInventDAO.findOne(id);
		return pdPo2Vo(invent);
	}
	
	public List<LmbConsumableInventDetail> findInvenDet(String id){
		List<LmbConsumableInventDetail> list = conInventDetDAO.findinventDet(id);
		return list;
	}
}
